﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using HtmlAgilityPack;
using System.Text.RegularExpressions;

namespace RaveEngine.Engine
{
    public class HtmlScrape : BaseScrape
    {
        #region Properties

        Regex regexRemoveHtmlXmlns = new Regex("<html\\b[^>]*>", RegexOptions.Compiled);

        /// <summary>
        /// Internal param exposing the output from the uriSource.
        /// </summary>
        public override string responseSource
        {
            get
            {
                if (m_htmlSource == null)
                {
                    HttpWebRequest request = HttpWebRequest.Create(uriSource) as HttpWebRequest;             
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        HtmlDocument hDoc = new HtmlDocument();
                        StringWriter outputWriter = new StringWriter();
                        hDoc.OptionOutputAsXml = true;
                        hDoc.OptionDefaultStreamEncoding = Encoding.UTF8;
                        hDoc.Load(responseStream);
                        hDoc.Save(outputWriter);
                        m_htmlSource = Encoding.UTF8.GetBytes(regexRemoveHtmlXmlns.Replace(outputWriter.ToString(), "<html>"));
                    }
                }
                return Encoding.UTF8.GetString(m_htmlSource);
            }
        }
        private byte[] m_htmlSource = null;

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="sUrl"></param>
        public HtmlScrape(string sUrl) : base(sUrl) { }

    }
}
